//
//  DoneDetailViewController.m
//  Findif
//
//  Created by Mac on 2019/5/6.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "DoneDetailViewController.h"
#import "HomeTableViewCell.h"
#import "GoodsModel.h"
#import "GoodsDetailsViewController.h"
#import "publishCommitViewController.h"
#import "MyOrderViewController.h"

@interface DoneDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, weak)UIView * topView;
@property (nonatomic, strong)UIImageView * doneImageView;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UILabel * exploreLabel;
@property (nonatomic, weak)UIButton * backButton;
@property (nonatomic, weak)UIButton * commitButton;
@property (nonatomic, strong)UIImageView * likeImageView;
@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray<GoodsModel *> * goodsList;

@end

@implementation DoneDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"订单核销";
    self.view.backgroundColor = ColorWithRGB(0xF8F9FA);
    [self loadData];
    [self setupUI];
}

- (void)setGoodsList:(NSArray<GoodsModel *> *)goodsList{
    
    _goodsList = goodsList;
    
    [self.tableView reloadData];
}

- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getSimilarMerchantsGoodsListsWithGoods_id:self.goods_id WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.goodsList = [NSArray yy_modelArrayWithClass:[GoodsModel class] json:responseData[@"data"]];
        }
    }];
}

- (void)setupUI{
    
    UIView * topView = [UIView new];
    
    topView.backgroundColor = ZRWhiteColor;
    topView.layer.cornerRadius = 5 * SCREENH6;
    topView.layer.masksToBounds = YES;
    topView.userInteractionEnabled = YES;
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    self.doneImageView = [self setupImageViewWithImg:@"成功"];
    [self.topView addSubview:self.doneImageView];
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"核销成功";
    titleLabel.textColor = ZRBlackColor;
    titleLabel.font = [UIFont systemFontOfSize:22 * SCREENH6];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.titleLabel = titleLabel;
    
    [self.topView addSubview:self.titleLabel];
    
    
    UILabel * exploreLabel = [UILabel new];
    
    exploreLabel.text = @"消费成功稍后可在-我的订单-中查看您的订单状态";
    exploreLabel.textColor = [UIColor lightGrayColor];
    exploreLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    exploreLabel.textAlignment = NSTextAlignmentCenter;
    
    self.exploreLabel = exploreLabel;
    
    [self.topView addSubview:self.exploreLabel];
    
    
    UIButton * backButton = [UIButton new];
    
    [backButton setTitle:@"我的订单" forState:UIControlStateNormal];
    [backButton setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    backButton.backgroundColor = ZRBlackColor;
    backButton.layer.cornerRadius = 5 * SCREENH6;
    backButton.layer.masksToBounds = YES;
    [backButton addTarget:self action:@selector(clickTopViewButton:) forControlEvents:UIControlEventTouchUpInside];
    backButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.backButton = backButton;
    
    [self.topView addSubview:self.backButton];
    
    
    UIButton * commitButton = [UIButton new];
    
    [commitButton setTitle:@"去评论" forState:UIControlStateNormal];
    [commitButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    commitButton.backgroundColor = ColorWithRGB(0xFFE035);
    commitButton.layer.cornerRadius = 5 * SCREENH6;
    commitButton.layer.masksToBounds = YES;
    [commitButton addTarget:self action:@selector(clickTopViewButton:) forControlEvents:UIControlEventTouchUpInside];
    commitButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.commitButton = commitButton;
    
    [self.topView addSubview:self.commitButton];
    
    self.likeImageView = [self setupImageViewWithImg:@"猜你喜欢"];
    [self.view addSubview:self.likeImageView];
    
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topHeight + 303 * SCREENH6, kScreenW, kScreenH - topHeight - 303 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF8F9FA);
    tableView.rowHeight = 120 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"DoneGoodsCell"];
    
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.goodsList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DoneGoodsCell" forIndexPath:indexPath];
    
    cell.model = self.goodsList[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HiddenBar;
    
    GoodsModel * model = self.goodsList[indexPath.row];
    GoodsDetailsViewController * detailVC = [GoodsDetailsViewController new];
    detailVC.ID = model.id;
    detailVC.infoType = [NSString stringWithFormat:@"%@",model.merchants_goods_type];
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(topHeight + 10 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.height.mas_equalTo(245 * SCREENH6);
    }];
    
    [self.doneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.topView);
        make.top.mas_equalTo(self.topView).offset(20 * SCREENH6);
        make.height.width.mas_equalTo(70 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.doneImageView);
        make.top.mas_equalTo(self.doneImageView.mas_bottom).offset(25 * SCREENH6);
    }];
    
    [self.exploreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.doneImageView);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(12 * SCREENH6);
        make.width.mas_equalTo(325 * SCREENH6);
    }];
    
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
        make.top.mas_equalTo(self.exploreLabel.mas_bottom).offset(25 * SCREENH6);
        make.leading.mas_equalTo(self.topView).offset(50 * SCREENH6);
    }];
    
    [self.commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerY.mas_equalTo(self.backButton);
        make.trailing.mas_equalTo(self.topView).offset(-50 * SCREENH6);
    }];
    
    [self.likeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(345 * SCREENH6);
        make.height.mas_equalTo(18 * SCREENH6);
        make.top.mas_equalTo(self.topView.mas_bottom).offset(20 * SCREENH6);
    }];
}



- (UIImageView *)setupImageViewWithImg:(NSString *)img{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:img];
    
    return imageView;
}


- (void)clickTopViewButton:(UIButton *)button{
    
    if (button == self.commitButton) {
        
        HiddenBar;
        publishCommitViewController * pushVC = [publishCommitViewController new];
        pushVC.ID = self.model.id;
        NSArray *array = [self.model.merchants_goods_img componentsSeparatedByString:@","];
        NSString * goodsStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,array.firstObject];
        NSURL * goodsUrl = [NSURL URLWithString:goodsStr];
        pushVC.goodUrl = goodsUrl;
        [self.navigationController pushViewController:pushVC animated:YES];
    }else{
        
        for (UIViewController *controller in self.navigationController.viewControllers) {
            if ([controller isKindOfClass:[MyOrderViewController class]]) {
                
                MyOrderViewController *A =(MyOrderViewController *)controller;
                [self.navigationController popToViewController:A animated:YES];
            }
        }
    }
}

@end
